package com.itxh.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectUtils {
    public static Object newInstance(Class clazzType){
        try {
            Constructor constructor = clazzType.getConstructor();
            return constructor.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void setProperty(Class clazzType,Object bean,String name,Object value){
        try {// 内省技术  -->  反射
            // 内省技术 根据属性名 --》 获取属性的getter或者setter方法
            Field field = clazzType.getDeclaredField(name);
            field.setAccessible(true);
            field.set(bean,value);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void invokeMethod(Class<?> clazzType, Object bean, String initMethod) {
        try {
            Method method = clazzType.getMethod(initMethod);
            method.invoke(bean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Class resolveClassName(String className) {
        try {
            return Class.forName(className);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static Class resolveType(Class clazzType, String name) {
        try{
            Field field = clazzType.getDeclaredField(name);
            field.setAccessible(true);
            return field.getType();

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
